﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text.RegularExpressions; 
namespace _16._02._13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            page_code();
            pogoda();
        }
        //погода
        private void pogoda() 
        {
            String data = DateTime.Now.Date.ToShortDateString();

            String url = "http://msk.nuipogoda.ru";

            url += data;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            StreamReader myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream());
            String html = myStreamReader.ReadToEnd();

            String pattern = "<div><h2>Температура:</h2>+2&deg;C</div><div>пасмурно, без осадков</div> <div><h2>Ветер:</h2>ЮЮЗ, 2 м/с</div> </div>  "; 
            Match match = Regex.Match(html, pattern);
            label8.Text = "Сегодня погода в москве " + match.Groups[1].ToString() ;
        }
        //курс
        private void page_code() 
        {String data = DateTime.Now.Date.ToShortDateString();
             
            String url = "http://www.cbr.ru/currency_base/D_print.aspx?date_req=";
           
            url += data;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            StreamReader myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream()); 
            String html = myStreamReader.ReadToEnd();
          
            String pattern = "Доллар США</td>\r\n<td align=\"right\">(.*)</td>";
           
            Match match = Regex.Match(html, pattern);
            label7.Text = "Сегодня доллар стоит " + match.Groups[1].ToString() + " рублей";

        }
        private string GetPublicIp()//айпи в инете
        {

            WebRequest wr = WebRequest.Create("http://checkip.dyndns.org/");
            WebResponse wr2 = wr.GetResponse();
            StreamReader sr = new StreamReader(wr2.GetResponseStream());
            string IPpage = sr.ReadToEnd();
            int start = IPpage.IndexOf("s: ") + 3;
            int finish = IPpage.LastIndexOf("</body>");
            return IPpage.Substring(start, finish - start);

        }




        private void btn1_Click(object sender, EventArgs e)
        {
            tb1.Text = Dns.GetHostName();//имя локалки
            string hostname = Dns.GetHostName();
            GetLocalIp();
            tb3.Text = GetPublicIp();//айпи в инете(вызов функции)


        }

        private void GetLocalIp()
        {
            string hostname = Dns.GetHostName();

            foreach (IPAddress IP in Dns.GetHostEntry(hostname).AddressList)
            {
                tb2.Text += "\t" + IP.ToString();
            }




        }
        //чат начало
        TcpListener lester;
        TcpClient clenter;
        StreamReader reader; 
        StreamWriter writer;
        String str;

        private void btnlisten_Click(object sender, EventArgs e)
        {
            StartLisaneng(); 
        }
        private void StartLisaneng() 
        {
            lester = new TcpListener(IPAddress.Any,Convert.ToInt32(tBlesten.Text));
            lester.Start();
            bW.RunWorkerAsync(); 
               
        }

        private void bW_DoWork(object sender, DoWorkEventArgs e)
        {
           clenter= lester.AcceptTcpClient();
         reader = new StreamReader(clenter.GetStream());
         str = reader.ReadToEnd();
         reader.Close();
        }

        private void bW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lBmassages.Items.Add(">"+str);
            bW.RunWorkerAsync();

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
        private void sand()
        {
            try
            {
                TcpClient sender = new TcpClient();
                sender.Connect(IPAddress.Parse(tBsenderIp.Text), Convert.ToInt32(tBsenderport.Text));
                writer = new StreamWriter(sender.GetStream());
                writer.Write(tBmassage);
                writer.Close();
            }
            catch 
            {
            }
        }
    }
}

